<template>
  <div class="order-success">
    <div class="container">
      <el-result
        icon="success"
        title="下单成功！"
        sub-title="您的订单已成功提交，我们会尽快为您发货。"
      >
        <template #extra>
          <el-button type="primary" @click="goToOrderDetail">查看订单详情</el-button>
          <el-button @click="$router.push('/products')">继续购物</el-button>
        </template>
      </el-result>

      <el-card v-if="order" class="order-info-card">
        <template #header>
          <span>订单信息</span>
        </template>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="订单号">{{ order.id }}</el-descriptions-item>
          <el-descriptions-item label="下单时间">{{ formatDate(order.createTime) }}</el-descriptions-item>
          <el-descriptions-item label="收货人">{{ order.receiverName }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ order.receiverPhone }}</el-descriptions-item>
          <el-descriptions-item label="收货地址" :span="2">{{ order.receiverAddress }}</el-descriptions-item>
          <el-descriptions-item label="订单总价" :span="2">
            <span class="total-price">¥{{ order.totalPrice }}</span>
          </el-descriptions-item>
        </el-descriptions>
        <div class="order-items">
          <h3>商品列表</h3>
          <el-table :data="order.items" border stripe>
            <el-table-column prop="productName" label="商品名称" />
            <el-table-column prop="productPrice" label="单价" width="100">
              <template #default="scope">
                ¥{{ scope.row.productPrice }}
              </template>
            </el-table-column>
            <el-table-column prop="quantity" label="数量" width="80" />
            <el-table-column prop="subtotal" label="小计" width="100">
              <template #default="scope">
                ¥{{ scope.row.subtotal.toFixed(2) }}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getOrderDetail } from '@/api/order'

const route = useRoute()
const router = useRouter()
const order = ref(null)

const orderId = route.params.orderId

const fetchOrder = async () => {
  if (!orderId) return
  const response = await getOrderDetail(orderId)
  if (response.data) {
    order.value = response.data
  }
}

const goToOrderDetail = () => {
  router.push(`/order/${orderId}`)
}

const formatDate = (dateString) => {
  if (!dateString) return '未知'
  return new Date(dateString).toLocaleString('zh-CN')
}

onMounted(() => {
  fetchOrder()
})
</script>

<style scoped>
.order-success {
  padding: 20px 0;
}
.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 20px;
}
.order-info-card {
  margin-top: 30px;
}
.total-price {
  color: #e74c3c;
  font-size: 20px;
  font-weight: bold;
}
.order-items {
  margin-top: 20px;
}
</style> 